/*
============================================================================
 Name        : pd_file_test.c
 Author      : Jian (raphyer@hotmail.com)
 Version     :
 Copyright   : GPL
 Description : pd_file_test.c - source file
============================================================================
*/

// INCLUDE FILES
#include "pd_file.h"

#define __TEST_FILE_NAME L"c:\\test\\test.log"

DD_BOOL _test_file_open_close() 
{
    pd_file* test_file = NULL ;
    
    /* Test PD_FILE_READ_WRITE */
    test_file = pd_file_open(__TEST_FILE_NAME, PD_FILE_READ_WRITE) ;

    __DD_ASSERT_DEFENSE(test_file, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_exist(__TEST_FILE_NAME), return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_delete(__TEST_FILE_NAME) < 0, return DD_FALSE) ;

    pd_file_close(test_file) ;
    test_file = DD_NULL ;
    
    __DD_ASSERT_DEFENSE(pd_file_exist(__TEST_FILE_NAME), return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_delete(__TEST_FILE_NAME) == 0, return DD_FALSE) ;
    
    /* Test PD_FILE_READ */
    test_file = pd_file_open(__TEST_FILE_NAME, PD_FILE_READ) ;

    __DD_ASSERT_DEFENSE(!test_file, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(!pd_file_exist(__TEST_FILE_NAME), return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_delete(__TEST_FILE_NAME) < 0, return DD_FALSE) ;

    
    /* Test PD_FILE_CREATE */
    test_file = pd_file_open(__TEST_FILE_NAME, PD_FILE_CREATE) ;

    __DD_ASSERT_DEFENSE(test_file, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_exist(__TEST_FILE_NAME), return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_delete(__TEST_FILE_NAME) < 0, return DD_FALSE) ;

    pd_file_close(test_file) ;
    test_file = DD_NULL ;

    __DD_ASSERT_DEFENSE(pd_file_exist(__TEST_FILE_NAME), return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_delete(__TEST_FILE_NAME) >= 0, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(!pd_file_exist(__TEST_FILE_NAME), return DD_FALSE) ;

    return DD_TRUE ;
}


#define __BUF_SIZE  100
#define __TEST_SIZE 20

DD_BOOL _test_file_read_write_seek() 
{
    pd_file* test_file = NULL ;
    void*    write_buf = DD_MALLOC(__BUF_SIZE) ;
    void*    read_buf  = DD_MALLOC(__BUF_SIZE) ;
    
    /* Test write */
    DD_MEMSET(write_buf, 1, __BUF_SIZE) ;

    test_file = pd_file_open(__TEST_FILE_NAME, PD_FILE_READ_WRITE) ; 
    __DD_ASSERT_DEFENSE(test_file, return DD_FALSE) ;
    
    __DD_ASSERT_DEFENSE(pd_file_write(test_file, write_buf, __BUF_SIZE) == __BUF_SIZE, return DD_FALSE) ;
    
    /* Test seek */

    __DD_ASSERT_DEFENSE(pd_file_size(test_file) == __BUF_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_tell(test_file) == __BUF_SIZE, return DD_FALSE) ;
    
    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, -(__BUF_SIZE * 2), PD_FILE_CURRENT) == 0, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, (__BUF_SIZE * 2), PD_FILE_BEGIN) == __BUF_SIZE, return DD_FALSE) ;
    
    __DD_ASSERT_DEFENSE(pd_file_tell(test_file) == __BUF_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, 1, PD_FILE_CURRENT) == __BUF_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, 1, PD_FILE_END) == __BUF_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, __BUF_SIZE + 1, PD_FILE_BEGIN) == __BUF_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_tell(test_file) == __BUF_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, -1, PD_FILE_CURRENT) == __BUF_SIZE - 1, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, 1, PD_FILE_CURRENT) == __BUF_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, -1, PD_FILE_END) == __BUF_SIZE - 1, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, 1, PD_FILE_END) == __BUF_SIZE , return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, 0, PD_FILE_BEGIN) == 0, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, -1, PD_FILE_BEGIN) == 0, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, -1, PD_FILE_CURRENT) == 0, return DD_FALSE) ;

    
    /* Test read/write */
    __DD_ASSERT_DEFENSE(pd_file_read(test_file, read_buf, __BUF_SIZE) == __BUF_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(DD_MEMCMP(read_buf, write_buf, __BUF_SIZE) == 0, return DD_FALSE) ;
    
    __DD_ASSERT_DEFENSE(pd_file_tell(test_file) == __BUF_SIZE, return DD_FALSE) ;

    DD_MEMSET(write_buf, 2, __BUF_SIZE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, -__TEST_SIZE, PD_FILE_CURRENT) == __BUF_SIZE - __TEST_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_write(test_file, write_buf, __BUF_SIZE) == __BUF_SIZE, return DD_FALSE) ;
    
    __DD_ASSERT_DEFENSE(pd_file_tell(test_file) == __BUF_SIZE + __BUF_SIZE - __TEST_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_seek(test_file, -__BUF_SIZE, PD_FILE_CURRENT) == __BUF_SIZE - __TEST_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(pd_file_read(test_file, read_buf, __BUF_SIZE) == __BUF_SIZE, return DD_FALSE) ;

    __DD_ASSERT_DEFENSE(DD_MEMCMP(read_buf, write_buf, __BUF_SIZE) == 0, return DD_FALSE) ;
    
    __DD_ASSERT_DEFENSE(pd_file_size(test_file) == __BUF_SIZE + __BUF_SIZE - __TEST_SIZE, return DD_FALSE) ;

    pd_file_close(test_file) ;
    
    __DD_ASSERT_DEFENSE(pd_file_delete(__TEST_FILE_NAME) >= 0, return DD_FALSE) ;

    DD_FREE(read_buf) ;
    DD_FREE(write_buf) ;

    return DD_TRUE ;
}


DD_BOOL pd_file_test() 
{
    if (!_test_file_open_close()) {
        DD_LOG("_test_file_open_close FAILED") ;
        return DD_FALSE ;
    } 

    if (!_test_file_read_write_seek()) {
        DD_LOG("_test_file_read_write_seek FAILED") ;
        return DD_FALSE ;
    } 

    return DD_TRUE ;
}

/* End of File */
